package com.ce.common.db.loader;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.ce.common.db.base.DataObject;
import com.ce.common.db.base.JDBCConnection;
import com.ce.common.db.loader.DatabaseLoader;
import com.ce.common.db.loader.impl.DatabaseLoaderImpl;

/**
 * 测试加载数据库读取类
 * @author yangenxiong
 *
 */
public class DatabaseLoaderT {

	DatabaseLoader loader;
		
	@Before
	public void setUp() throws Exception {
		loader = new DatabaseLoaderImpl(JDBCConnection.getJDBCConnection());
	}
	
	@After
	public void tearDown() {
		loader = null;
	}

	/**
	 * 测试获取需要导出的表名
	 * @author yangenxiong
	 */
	@Test
	public void testGetDumpTables() throws Exception {
		List<DataObject> result = loader.getTables(new String[]{"TABLE", "VIEW"}, null);
		assertEquals(result.size(), 252);
		
		List<String> ignore = new ArrayList<String>();
		ignore.add("account_account");//忽略一个不需要的表
		result = loader.getTables(new String[]{"TABLE", "VIEW"}, ignore);
		assertEquals(result.size(), 251);
	}
	
	/**
	 * 测试获取所有的表
	 * @throws Exception
	 * @author yangenxiong
	 */
	@Test
	public void testGetAllTables() throws Exception {
		List<DataObject> result = loader.getAllTables(new String[]{"TABLE"});
		//System.out.println(result.size());
		assertEquals(result.size(), 216);
		
		//测试数据中有18个视图
		result = loader.getAllTables(new String[]{"VIEW"});
		assertEquals(result.size(), 36);
		
		result = loader.getAllTables(new String[]{"TABLE", "VIEW"});
		assertEquals(result.size(), 252);
	}
	
	/**
	 * 测试根据前缀获取数据库的表
	 * @throws Exception
	 * @author yangenxiong
	 */
	@Test
	public void testGetTablesByRegs() throws Exception {
		List<DataObject> result = loader.getTablesByPrefixs(new String[]{"TABLE", "VIEW"}, new String[]{"jbpm"});
		assertEquals(result.size(), 29);
		
		result = loader.getTablesByPrefixs(new String[]{"TABLE", "VIEW"}, new String[]{"jbpm", "org"});
		assertEquals(result.size(), 51);
	}
	
	/**
	 * 测试获取不符合前缀表
	 * @throws Exception
	 * @author yangenxiong
	 */
	@Test
	public void testGetTablesWithOutRegs() throws Exception  {
		List<DataObject> result = loader.getTablesWithOutPrefixs(new String[]{"TABLE", "VIEW"}, new String[]{"jbpm"});
		assertEquals(result.size(), 223);
		
		result = loader.getTablesWithOutPrefixs(new String[]{"TABLE", "VIEW"}, new String[]{"jbpm", "org"});
		assertEquals(result.size(), 201);
	}
}
